package org.openzip.openzip.gui.util;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.openzip.jarsfx.IRefreshablePacker;

public class ProgressWindow extends JFrame implements IRefreshablePacker {
	
	private JProgressBar fileProgressBar;
	private JProgressBar totalProgressBar;
	private JPanel fileProgressPanel;
	private JPanel totalProgressPanel;
	private JLabel fileBytes;
	private JLabel totalBytes;
	
	private String fileName;
	private String fileUnits = " B";
	private long fileMax;
	
	// This will be amount of completed data from COMPLETED files (not from the
	// current one).
	private long completedTotal;
	private long totalMax;
	private String totalUnits = " B";
	
	public ProgressWindow() {
		super("Compression progress...");
		this.setLayout(new BorderLayout());
		
		fileProgressPanel = new JPanel();
		fileProgressPanel.setLayout(new BorderLayout());
		fileProgressPanel.setBorder(
				BorderFactory.createTitledBorder("File Progress"));
		fileBytes = new JLabel("0 of 100MB Completed from file: test.txt");
		fileProgressPanel.add(fileBytes, BorderLayout.NORTH);
		fileProgressBar = new JProgressBar(0, 100);
		fileProgressBar.setStringPainted(true);
		fileProgressPanel.add(fileProgressBar, BorderLayout.SOUTH);
		
		totalProgressPanel = new JPanel();
		totalProgressPanel.setLayout(new BorderLayout());
		totalProgressPanel.setBorder(
				BorderFactory.createTitledBorder("Total Progress"));
		totalBytes = new JLabel("0 of 100MB Completed total");
		totalProgressPanel.add(totalBytes, BorderLayout.SOUTH);
		totalProgressBar = new JProgressBar(0, 100);
		totalProgressBar.setStringPainted(true);
		totalProgressPanel.add(totalProgressBar);
		
		this.add(fileProgressPanel, BorderLayout.NORTH);
		this.add(totalProgressPanel, BorderLayout.SOUTH);
		
		this.setPreferredSize(new Dimension(600, 150));
		this.pack();
		
		//this.setSize(new Dimension(400, 150));
		
		completedTotal = 0;
		fileMax = 0;
		
		// static testing values
		/*setFile("Pokus.jpg", 200);
		setFileCompletedValue(134);
		completedTotal = 14;
		setTotalSize(455);
		updateTotalText(134);*/
		
	}
	
	/** Sets size of all the data to be processed (all files)
	 * 
	 * @param size Size of all items to be processed
	 */
	public void setTotalSize(long size) {
		totalProgressBar.setMaximum((int)size);
		totalMax = size;
	}
	
	/** This method sets the name and size of currently compressed file
	 * 
	 * @param fileName Name of the file being processed
	 * @param size Size Size of the current file
	 */
	public void setFile(String fileName, long size) {
		//First we assume the previous item has been completed and update total
		completedTotal += fileMax;
		
		this.fileName = fileName;
		this.fileMax = size;
		
		//Strip decimal part
		fileProgressBar.setMaximum((int)fileMax / 1);
		updateFileText(0);
	}
	
	/** Sets the completed amount of the current file
	 * 
	 * @param completed Completed portion
	 */
	public void setFileCompletedValue(long completed) {
		fileProgressBar.setValue((int)completed);
		totalProgressBar.setValue((int)(completedTotal + completed));
		updateFileText(completed);
		updateTotalText(completed);
	}
	
	public void taskCompleted() {
		JOptionPane.showMessageDialog(null, "All files have been packed.");
		this.dispose();
	}
	
//=============== Private methods and testing main() ===========================
	
	//updates the JLabel with information about file
	private void updateFileText(long completed) {
		fileBytes.setText(Long.toString(completed) +
				" of " + Long.toString((int)fileMax) + fileUnits + 
				" Completed from file: " + fileName);
	}
	
	// completed parameter is amount of data compressed from current file
	private void updateTotalText(long completed) {
		totalBytes.setText(Long.toString(completed + completedTotal) +
				" of " + Long.toString(totalMax) + totalUnits + 
				" Completed total");
	}
	
	//testing
	public static void main(String[] args) {
		ProgressWindow window = new ProgressWindow();
		window.setVisible(true);
		
		String[] fileNames = {"test1.txt", "test2.txt", "test3.jpg"};
		int[] fileSizes = {10250, 5111, 3456};
		
		window.setTotalSize(fileSizes[0]+fileSizes[1]+fileSizes[2]);
		
		for (int fileIndex=0; fileIndex<fileNames.length; fileIndex++) {
			window.setFile(fileNames[fileIndex], fileSizes[fileIndex]);
			
			int completed = 0;
			int incrementSize = 1000;
			while (completed < fileSizes[fileIndex]+incrementSize) {
				window.setFileCompletedValue(completed);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) { }
				completed += incrementSize;
			}
		}
		
		window.taskCompleted();
		

	}
}
